package com.cwf.business.util;

import android.content.Context;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.support.annotation.ColorInt;
import android.support.annotation.DrawableRes;
import android.support.annotation.Nullable;
import android.support.annotation.StringRes;
import android.support.v7.widget.Toolbar;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.cwf.business.R;

/**
 * Created by sam_chan on 2017/5/8.
 */
public class DFToolbar extends Toolbar {

    private TextView mLeftTextView;
    private TextView mTitleTextView;
    private TextView mRightTextView;
    private ImageView mRightImageView;
    private ImageView mNavigationButton;
    private View mLine;
    private String mTitleText;


    public DFToolbar(Context context) {
        this(context, null);
    }

    public DFToolbar(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public DFToolbar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        View.inflate(context, R.layout.fe_toolbar_container, this);
        mLine = findViewById(R.id.toolBarLine);
        mLeftTextView = (TextView) findViewById(R.id.toolBarLeftTextView);
        mTitleTextView = (TextView) findViewById(R.id.toolBarTitleTextView);
        mRightTextView = (TextView) findViewById(R.id.toolBarRightTextView);
        mRightImageView = (ImageView) findViewById(R.id.toolBarRightImageView);
        mNavigationButton = (ImageView) findViewById(R.id.toolbarNavigation);
    }

    @Override
    public void setNavigationIcon(@DrawableRes int resId) {
        setNavigationIcon(getResources().getDrawable(resId));
    }

    @Override
    public void setNavigationIcon(@Nullable Drawable icon) {
        mNavigationButton.setVisibility(View.VISIBLE);
        mNavigationButton.setImageDrawable(icon);
    }

    public void setNavigationVisibility(int visibility) {
        mNavigationButton.setVisibility(visibility);
    }

    @Override
    public void setNavigationOnClickListener(OnClickListener listener) {
        mNavigationButton.setOnClickListener(listener);
    }

    public void showNavigationIcon() {
        mNavigationButton.setVisibility(VISIBLE);
        mLeftTextView.setVisibility(GONE);
    }

    @Override
    public void setTitleTextColor(@ColorInt int color) {
        mTitleTextView.setTextColor(color);
        invalidate();
    }

    @Override
    public void setTitle(@StringRes int resId) {
        setTitle(getResources().getString(resId));
    }

    @Override
    public void setTitle(CharSequence title) {
        setTitleWithNavigation(title, true);
    }

    public void setTitleWithNavigation(CharSequence title, boolean withNavigation) {
        mTitleText = title.toString();
        mTitleTextView.setText(title);

        if (withNavigation) {
            Drawable navigation = getResources().getDrawable(R.drawable.icon_back);
            navigation.setColorFilter(Color.parseColor("#484848"), PorterDuff.Mode.SRC_ATOP);
            setNavigationIcon(navigation);
        }
        invalidate();
    }

    @Override
    public CharSequence getTitle() {
        return mTitleText;
    }


    public void setLeftText(String text) {
        mLeftTextView.setText(text);
        mLeftTextView.setVisibility(View.VISIBLE);
        mNavigationButton.setVisibility(View.GONE);
        invalidate();
    }

    public void setLeftTextClickListener(OnClickListener onClickListener) {
        mLeftTextView.setOnClickListener(onClickListener);
    }

    public String getleftText() {
        return mLeftTextView.getText().toString();
    }

    public TextView getleftTextView() {
        return this.mLeftTextView;
    }

    public void showLeftText() {
        mLeftTextView.setVisibility(VISIBLE);
        mNavigationButton.setVisibility(GONE);
    }


    // *************************************** Right Image Start. *****************************************
    public void setRightIcon(@DrawableRes int resId) {
        setRightIcon(getResources().getDrawable(resId));
    }

    public void setRightIcon(Drawable drawable) {
        mRightImageView.setImageDrawable(drawable);
        mRightTextView.setVisibility(View.GONE);
        mRightImageView.setVisibility(View.VISIBLE);
        invalidate();
    }

    public void setRightIconVisibility(int visibility) {
        mRightImageView.setVisibility(visibility);
    }

    public void showRightIcon(){
        mRightImageView.setVisibility(VISIBLE);
        mRightTextView.setVisibility(GONE);
    }

    public void setRightImageClickListener(OnClickListener onClickListener) {
        mRightImageView.setOnClickListener(onClickListener);
    }

    public void setRightText(@StringRes int resId) {
        setRightText(getResources().getString(resId));
    }

    public void setRightTextColor(int color) {
        mRightTextView.setTextColor(color);
    }

    public void setRightText(String text) {
        mRightTextView.setText(text);
        mRightTextView.setVisibility(View.VISIBLE);
        mRightImageView.setVisibility(View.GONE);
        invalidate();
    }

    public void setRightTextClickListener(OnClickListener onClickListener) {
        mRightTextView.setOnClickListener(onClickListener);
    }

    public String getRightText() {
        return mRightTextView.getText().toString();
    }

    public TextView getRightTextView() {
        return this.mRightTextView;
    }

    public void setRightTextVisbility(int visibility) {
        mRightTextView.setVisibility(visibility);
    }

    public void setLineVisibility(int visibility) {
        mLine.setVisibility(visibility);
    }

    public void setLightMode() {
        changeStatsBarMode(Color.BLACK);
    }

    public void setDarkMode() {
        changeStatsBarMode(Color.WHITE);
    }

    private void changeStatsBarMode(int color) {
        mTitleTextView.setTextColor(color);

        if (mNavigationButton.getVisibility() == View.VISIBLE) {
            mNavigationButton.setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
        }

        if (mRightTextView.getVisibility() == View.VISIBLE) {
            mRightTextView.setTextColor(color);
        }

        if (mRightImageView.getVisibility() == View.VISIBLE) {
            mRightImageView.setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
        }

        invalidate();
    }
}
